home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / sfxfd.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.1 KB  |  396 lines

  1. /* Copyright (C) 1994, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: sfxfd.c,v 1.5 2000/09/19 19:00:49 lpd Exp $ */
  20. /* File stream implementation using direct OS calls */
  21. /******
  22.  ****** NOTE: THIS FILE MAY NOT COMPILE ON NON-UNIX PLATFORMS, AND MAY
  23.  ****** REQUIRE EDITING ON SOME UNIX PLATFORMS.
  24.  ******/
  25. #include "stdio_.h"        /* includes std.h */
  26. #include "errno_.h"
  27. #include "memory_.h"
  28. /*
  29.  * It's likely that you will have to edit the next line on some Unix
  30.  * and most non-Unix platforms, since there is no standard (ANSI or
  31.  * otherwise) for where to find these definitions.
  32.  */
  33. /*
  34.  * unistd.h may declare unlink in a way that conflicts with stdio_.h if
  35.  * const has been disabled.
  36.  */
  37. #define unlink unlink_
  38. #include <unistd.h>        /* for read, write, fsync, lseek */
  39. #include "gdebug.h"
  40. #include "gpcheck.h"
  41. #include "stream.h"
  42. #include "strimpl.h"
  43.  
  44. /*
  45.  * This is an alternate implementation of file streams.  It still uses
  46.  * FILE * in the interface, but it uses direct OS calls for I/O.
  47.  * It also includes workarounds for the nasty habit of System V Unix
  48.  * of breaking out of read and write operations with EINTR, EAGAIN,
  49.  * and/or EWOULDBLOCK "errors".
  50.  *
  51.  * The interface should be identical to that of sfxstdio.c.  However, in
  52.  * order to allow both implementations to exist in the same executable, we
  53.  * optionally use different names for sread_file, swrite_file, and
  54.  * sappend_file, and omit sread_subfile (the public procedures).
  55.  * See sfxboth.c.
  56.  */
  57. #define KEEP_FILENO_API
  58. #ifdef KEEP_FILENO_API
  59. /* Provide prototypes to avoid compiler warnings. */
  60. void
  61.     sread_fileno(P4(stream *, FILE *, byte *, uint)),
  62.     swrite_fileno(P4(stream *, FILE *, byte *, uint)),
  63.     sappend_fileno(P4(stream *, FILE *, byte *, uint));
  64. #else
  65. #  define sread_fileno sread_file
  66. #  define swrite_fileno swrite_file
  67. #  define sappend_fileno sappend_file
  68. #endif
  69.  
  70. /* Forward references for file stream procedures */
  71. private int
  72.     s_fileno_available(P2(stream *, long *)),
  73.     s_fileno_read_seek(P2(stream *, long)),
  74.     s_fileno_read_close(P1(stream *)),
  75.     s_fileno_read_process(P4(stream_state *, stream_cursor_read *,
  76.                  stream_cursor_write *, bool));
  77. private int
  78.     s_fileno_write_seek(P2(stream *, long)),
  79.     s_fileno_write_flush(P1(stream *)),
  80.     s_fileno_write_close(P1(stream *)),
  81.     s_fileno_write_process(P4(stream_state *, stream_cursor_read *,
  82.                   stream_cursor_write *, bool));
  83. private int
  84.     s_fileno_switch(P2(stream *, bool));
  85.  
  86. /* Get the file descriptor number of a stream. */
  87. inline private int
  88. sfileno(const stream *s)
  89. {
  90.     return fileno(s->file);
  91. }
  92.  
  93. /* Get the current position of a file descriptor. */
  94. inline private long
  95. ltell(int fd)
  96. {
  97.     return lseek(fd, 0L, SEEK_CUR);
  98. }
  99.  
  100. /* Define the System V interrupts that require retrying a call. */
  101. private bool
  102. errno_is_retry(int errn)
  103. {
  104.     switch (errn) {
  105. #ifdef EINTR
  106.     case EINTR: return true;
  107. #endif
  108. #if defined(EAGAIN) && (!defined(EINTR) || EAGAIN != EINTR)
  109.     case EAGAIN: return true;
  110. #endif
  111. #if defined(EWOULDBLOCK) && (!defined(EINTR) || EWOULDBLOCK != EINTR) && (!defined(EAGAIN) || EWOULDBLOCK != EAGAIN)
  112.     case EWOULDBLOCK: return true;
  113. #endif
  114.     default: return false;
  115.     }
  116. }
  117.  
  118. /* ------ File reading ------ */
  119.  
  120. /* Initialize a stream for reading an OS file. */
  121. void
  122. sread_fileno(register stream * s, FILE * file, byte * buf, uint len)
  123. {
  124.     static const stream_procs p = {
  125.     s_fileno_available, s_fileno_read_seek, s_std_read_reset,
  126.     s_std_read_flush, s_fileno_read_close, s_fileno_read_process,
  127.     s_fileno_switch
  128.     };
  129.     /*
  130.      * There is no really portable way to test seekability,
  131.      * but this should work on most systems.
  132.      */
  133.     int fd = fileno(file);
  134.     long curpos = ltell(fd);
  135.     bool seekable = (curpos != -1L && lseek(fd, curpos, SEEK_SET) != -1L);
  136.  
  137.     s_std_init(s, buf, len, &p,
  138.            (seekable ? s_mode_read + s_mode_seek : s_mode_read));
  139.     if_debug2('s', "[s]read file=0x%lx, fd=%d\n", (ulong) file,
  140.           fileno(file));
  141.     s->file = file;
  142.     s->file_modes = s->modes;
  143.     s->file_offset = 0;
  144.     s->file_limit = max_long;
  145. }
  146.  
  147. /* Confine reading to a subfile.  This is primarily for reusable streams. */
  148. /*
  149.  * We omit this procedure if we are also include sfxstdio.c, which has an
  150.  * identical definition.
  151.  */
  152. #ifndef KEEP_FILENO_API
  153. int
  154. sread_subfile(stream *s, long start, long length)
  155. {
  156.     if (s->file == 0 || s->modes != s_mode_read + s_mode_seek ||
  157.     s->file_offset != 0 || s->file_limit != max_long ||
  158.     ((s->position < start || s->position > start + length) &&
  159.      sseek(s, start) < 0)
  160.     )
  161.     return ERRC;
  162.     s->position -= start;
  163.     s->file_offset = start;
  164.     s->file_limit = length;
  165.     return 0;
  166. }
  167. #endif
  168.  
  169. /* Procedures for reading from a file */
  170. private int
  171. s_fileno_available(register stream * s, long *pl)
  172. {
  173.     long max_avail = s->file_limit - stell(s);
  174.     long buf_avail = sbufavailable(s);
  175.     int fd = sfileno(s);
  176.  
  177.     *pl = min(max_avail, buf_avail);
  178.     if (sseekable(s)) {
  179.     long pos, end;
  180.  
  181.     pos = ltell(fd);
  182.     if (pos < 0)
  183.         return ERRC;
  184.     end = ltell(fd);
  185.     if (lseek(fd, pos, SEEK_SET) < 0 || end < 0)
  186.         return ERRC;
  187.     buf_avail += end - pos;
  188.     *pl = min(max_avail, buf_avail);
  189.     if (*pl == 0)
  190.         *pl = -1;        /* EOF */
  191.     } else {
  192.     if (*pl == 0)
  193.         *pl = -1;        /* EOF */
  194.     }
  195.     return 0;
  196. }
  197. private int
  198. s_fileno_read_seek(register stream * s, long pos)
  199. {
  200.     uint end = s->srlimit - s->cbuf + 1;
  201.     long offset = pos - s->position;
  202.  
  203.     if (offset >= 0 && offset <= end) {  /* Staying within the same buffer */
  204.     s->srptr = s->cbuf + offset - 1;
  205.     return 0;
  206.     }
  207.     if (pos < 0 || pos > s->file_limit ||
  208.     lseek(sfileno(s), s->file_offset + pos, SEEK_SET) < 0
  209.     )
  210.     return ERRC;
  211.     s->srptr = s->srlimit = s->cbuf - 1;
  212.     s->end_status = 0;
  213.     s->position = pos;
  214.     return 0;
  215. }
  216. private int
  217. s_fileno_read_close(stream * s)
  218. {
  219.     FILE *file = s->file;
  220.  
  221.     if (file != 0) {
  222.     s->file = 0;
  223.     return (fclose(file) ? ERRC : 0);
  224.     }
  225.     return 0;
  226. }
  227.  
  228. /* Process a buffer for a file reading stream. */
  229. /* This is the first stream in the pipeline, so pr is irrelevant. */
  230. private int
  231. s_fileno_read_process(stream_state * st, stream_cursor_read * ignore_pr,
  232.               stream_cursor_write * pw, bool last)
  233. {
  234.     stream *s = (stream *)st;    /* no separate state */
  235.     int fd = sfileno(s);
  236.     uint max_count;
  237.     int nread, status;
  238.  
  239. again:
  240.     max_count = pw->limit - pw->ptr;
  241.     status = 1;
  242.     if (s->file_limit < max_long) {
  243.     long limit_count = s->file_offset + s->file_limit - ltell(fd);
  244.  
  245.     if (max_count > limit_count)
  246.         max_count = limit_count, status = EOFC;
  247.     }
  248.     /*
  249.      * In the Mac MetroWerks compiler, the prototype for read incorrectly
  250.      * declares the second argument of read as char * rather than void *.
  251.      * Work around this here.
  252.      */
  253.     nread = read(fd, (void *)(pw->ptr + 1), max_count);
  254.     if (nread > 0)
  255.     pw->ptr += nread;
  256.     else if (nread == 0)
  257.     status = EOFC;
  258.     else if (errno_is_retry(errno))    /* Handle System V interrupts */
  259.     goto again;
  260.     else
  261.     status = ERRC;
  262.     process_interrupts();
  263.     return status;
  264. }
  265.  
  266. /* ------ File writing ------ */
  267.  
  268. /* Initialize a stream for writing an OS file. */
  269. void
  270. swrite_fileno(register stream * s, FILE * file, byte * buf, uint len)
  271. {
  272.     static const stream_procs p = {
  273.     s_std_noavailable, s_fileno_write_seek, s_std_write_reset,
  274.     s_fileno_write_flush, s_fileno_write_close, s_fileno_write_process,
  275.     s_fileno_switch
  276.     };
  277.  
  278.     s_std_init(s, buf, len, &p,
  279.            (file == stdout ? s_mode_write : s_mode_write + s_mode_seek));
  280.     if_debug2('s', "[s]write file=0x%lx, fd=%d\n", (ulong) file,
  281.           fileno(file));
  282.     s->file = file;
  283.     s->file_modes = s->modes;
  284.     s->file_offset = 0;        /* in case we switch to reading later */
  285.     s->file_limit = max_long;    /* ibid. */
  286. }
  287. /* Initialize for appending to an OS file. */
  288. void
  289. sappend_fileno(register stream * s, FILE * file, byte * buf, uint len)
  290. {
  291.     swrite_fileno(s, file, buf, len);
  292.     s->modes = s_mode_write + s_mode_append;    /* no seek */
  293.     s->file_modes = s->modes;
  294.     s->position = lseek(fileno(file), 0L, SEEK_END);
  295. }
  296. /* Procedures for writing on a file */
  297. private int
  298. s_fileno_write_seek(stream * s, long pos)
  299. {
  300.     /* We must flush the buffer to reposition. */
  301.     int code = sflush(s);
  302.  
  303.     if (code < 0)
  304.     return code;
  305.     if (lseek(sfileno(s), pos, SEEK_SET) < 0)
  306.     return ERRC;
  307.     s->position = pos;
  308.     return 0;
  309. }
  310. private int
  311. s_fileno_write_flush(register stream * s)
  312. {
  313.     int result = s_process_write_buf(s, false);
  314.  
  315.      printf( "fsync here\n" );
  316.      discard(/*fsync(sfileno(s))*/0);
  317.     return result;
  318. }
  319. private int
  320. s_fileno_write_close(register stream * s)
  321. {
  322.     s_process_write_buf(s, true);
  323.     return s_fileno_read_close(s);
  324. }
  325.  
  326. /* Process a buffer for a file writing stream. */
  327. /* This is the last stream in the pipeline, so pw is irrelevant. */
  328. private int
  329. s_fileno_write_process(stream_state * st, stream_cursor_read * pr,
  330.                stream_cursor_write * ignore_pw, bool last)
  331. {
  332.     int nwrite, status;
  333.     uint count;
  334.  
  335. again:
  336.     count = pr->limit - pr->ptr;
  337.     /* Some versions of the DEC C library on AXP architectures */
  338.     /* give an error on write if the count is zero! */
  339.     if (count == 0) {
  340.     process_interrupts();
  341.     return 0;
  342.     }
  343.     /* See above regarding the Mac MetroWorks compiler. */
  344.     nwrite = write(sfileno((stream *)st), (const void *)(pr->ptr + 1), count);
  345.     if (nwrite >= 0) {
  346.     pr->ptr += nwrite;
  347.     status = 0;
  348.     } else if (errno_is_retry(errno))    /* Handle System V interrupts */
  349.     goto again;
  350.     else
  351.     status = ERRC;
  352.     process_interrupts();
  353.     return status;
  354. }
  355.  
  356. /* ------ File switching ------ */
  357.  
  358. /* Switch a file stream to reading or writing. */
  359. private int
  360. s_fileno_switch(stream * s, bool writing)
  361. {
  362.     uint modes = s->file_modes;
  363.     int fd = sfileno(s);
  364.     long pos;
  365.  
  366.     if (writing) {
  367.     if (!(s->file_modes & s_mode_write))
  368.         return ERRC;
  369.     pos = stell(s);
  370.     if_debug2('s', "[s]switch 0x%lx to write at %ld\n",
  371.           (ulong) s, pos);
  372.     lseek(fd, pos, SEEK_SET);    /* pacify OS */
  373.     if (modes & s_mode_append) {
  374.         sappend_file(s, s->file, s->cbuf, s->cbsize);  /* sets position */
  375.     } else {
  376.         swrite_file(s, s->file, s->cbuf, s->cbsize);
  377.         s->position = pos;
  378.     }
  379.     s->modes = modes;
  380.     } else {
  381.     if (!(s->file_modes & s_mode_read))
  382.         return ERRC;
  383.     pos = stell(s);
  384.     if_debug2('s', "[s]switch 0x%lx to read at %ld\n",
  385.           (ulong) s, pos);
  386.     if (sflush(s) < 0)
  387.         return ERRC;
  388.     lseek(fd, 0L, SEEK_CUR);    /* pacify OS */
  389.     sread_file(s, s->file, s->cbuf, s->cbsize);
  390.     s->modes |= modes & s_mode_append;    /* don't lose append info */
  391.     s->position = pos;
  392.     }
  393.     s->file_modes = modes;
  394.     return 0;
  395. }
  396.